package cn.ljn.org.service.impl;
import cn.ljn.basic.query.BaseQuery;
import cn.ljn.basic.service.impl.BaseServiceImpl;
import cn.ljn.basic.util.MD5Utils;
import cn.ljn.basic.util.PageList;
import cn.ljn.basic.util.StrUtils;
import cn.ljn.org.domain.Employee;
import cn.ljn.org.mapper.EmployeeMapper;
import cn.ljn.org.service.EmployeeService;
import cn.ljn.system.mapper.RoleMapper;
import cn.ljn.system.vo.EmpAndRoleVo;
import cn.ljn.user.domain.Logininfo;
import cn.ljn.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private RoleMapper  roleMapper;


    @Override
    public List<Employee> getEmployees(Long id) {
        return employeeMapper.getEmployees(id)   ;
    }


    @Override
    public void add(Employee employee) {
        // 新增员工
        // 设置盐值
        String randomString = StrUtils.getComplexRandomString(32);
        // 设置密码
        String password = MD5Utils.encrypByMd5(randomString + "1");
        employee.setSalt(randomString);
        employee.setPassword(password);
        // 添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        // 添加emp
        employee.setLogininfoId(logininfo.getId());
        System.out.println(logininfo.getId());
        super.add(employee);

        // 添加中间表
        roleMapper.addMiddle(employee.getId(),employee.getRoles());
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
        // 添加logininfo表
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfoMapper.update(logininfo);
        // 删除中间表-用户
        roleMapper.deleteByEmpId(employee.getId());
        // 添加中间表
        roleMapper.addMiddle(employee.getId(),employee.getRoles());
    }


    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> pageList = super.queryByPage(query);
        if (pageList == null || CollectionUtils.isEmpty(pageList.getRows())){
            return pageList;
        }
        List<Employee> empList = pageList.getRows();
        // 查询角色id集合
        List<Long> empIds = empList.stream().map(Employee::getId).collect(Collectors.toList());
        List<EmpAndRoleVo> empAndRoleVo = roleMapper.findRoleIdsByEIds(empIds);
        Map<Long, List<Long>> empAndRoleVoMap = empAndRoleVo.stream().collect(Collectors.groupingBy(EmpAndRoleVo::getEmployeeId,
                Collectors.mapping(EmpAndRoleVo::getRoleId, Collectors.toList())));
        empList.forEach(t->{
            List<Long> roleIds = empAndRoleVoMap.get(t.getId());
            if (!CollectionUtils.isEmpty(roleIds)){
                t.setRoles(roleIds);
            }
        });
        return pageList;
    }
}
